package com.sanhai.android.mvp;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sanhai.android.service.Token;
import com.sanhai.android.util.ABAppUtil;
import com.sanhai.android.util.Util;


/**
 * Created by mahuihuang on 15/3/30.
 */
public class BaseActivity extends FragmentActivity implements IBaseView {

    private static final String TAG = "BaseActivity";
    private static Toast toast = null;
    private static Dialog dialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Token.setContext(getApplicationContext());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ABAppUtil.hideSoftInput(this);
        return super.onTouchEvent(event);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//		super.onSaveInstanceState(outState);
    }

    public void back(View view) {
        finish();
    }

    public void setOnClickListener(int viewId, View.OnClickListener onClickListener) {
        View view = findViewById(viewId);
        if (view == null) {
            Log.w(TAG, "没有找到这个控件:" + view);
            return;
        }
        view.setOnClickListener(onClickListener);
    }

    public void setText(int viewId, String text) {
        View view = findViewById(viewId);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        } else {
            Log.w(TAG, "不确定的类型");
        }
    }

    public void setVisibility(int viewId, int visibility) {
        if (visibility != View.GONE && visibility != View.VISIBLE && visibility != View.INVISIBLE) {
            Log.w(TAG, "参数错误， visibility:" + visibility);
            return;
        }
        View view = findViewById(viewId);
        if (view == null) {
            Log.w(TAG, "没有找到这个控件:" + view);
            return;
        }
        view.setVisibility(visibility);
    }

    public void toggleVisible(int viewId) {
        View view = findViewById(viewId);
        if (view == null) {
            Log.w(TAG, "没有找到这个控件:" + view);
            return;
        }
        if (view.getVisibility() == View.VISIBLE) {
            view.setVisibility(View.GONE);
            view.setFocusable(false);
        } else {
            view.setFocusable(true);
            view.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void showToastMessage(final String message) {
    /*	runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (toast != null) {
					toast.cancel();
					toast = null;
				}
				toast = Toast.makeText(BaseActivity.this, message, Toast.LENGTH_LONG);
				toast.show();
			}
		});*/
        Util.toastMessage(this, message);
    }


    @Override
    public void showLoadingDialog(String message) {
        try {
            cancelLoadingDialog();
            if (TextUtils.isEmpty(message)) {
                message = "正在加载...";
            }
            dialog = ProgressDialog.show(this, null, message, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void cancelLoadingDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    public void onProgress(int bytesWritten, int totalSize) {

    }
}
